package com.example.dangerouscargodriver.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.entry.OrderStatusInfo;
import com.example.dangerouscargodriver.entry.ResourceInfo;
import com.example.dangerouscargodriver.ui.activity.MyOrderDetailInfoActivity;
import com.example.dangerouscargodriver.ui.activity.MyResourceDetailActivity;
import com.example.dangerouscargodriver.ui.activity.ResourceDetailActivity;

public class OrderInfoView extends LinearLayout {

    private static final String TAG = "OrderInfoView";
    private TextView mFromName = null;
    private TextView mToName = null;
    private TextView mSourceDistance = null;
    private TextView mPublishTime = null;
    private TextView mPublisher = null;
    private TextView mPublishState = null;
    private TextView mRegTime = null;
    private TextView mLoadTime = null;
    private TextView mLoadTimeTit = null;
    private Button mGrapOrder = null;
    private LinearLayout mPublishTimeLay;
    private TextView mMoney = null;
    private LinearLayout mMetaLayout = null;
    private Context mContext = null;

    private OrderStatusInfo mOrderInfo = null;

    public OrderInfoView(Context context, OrderStatusInfo info) {
        this(context, null, info);
    }

    public OrderInfoView(Context context, AttributeSet attrs, OrderStatusInfo info) {
        this(context, attrs, 0, info);
    }

    public OrderInfoView(Context context, AttributeSet attrs, int defStyleAttr, OrderStatusInfo info) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, info);
    }

    public void init(Context context, AttributeSet attrs, OrderStatusInfo info) {
        mContext = context;
        mOrderInfo = info;
        View itemLayout = LayoutInflater.from(context).inflate(R.layout.layout_order_info_item, this);
        mFromName = itemLayout.findViewById(R.id.item_start_city);
        mSourceDistance = itemLayout.findViewById(R.id.item_source_distance);
        mToName = itemLayout.findViewById(R.id.item_end_city);
        mPublishTime = itemLayout.findViewById(R.id.tv_publish_time);
        mPublisher = itemLayout.findViewById(R.id.tv_resource_publisher);
        mPublishState = itemLayout.findViewById(R.id.tv_order_state);
        mRegTime = itemLayout.findViewById(R.id.item_register_time);
        mGrapOrder = itemLayout.findViewById(R.id.btn_grab_order);
        mPublishTimeLay = itemLayout.findViewById(R.id.lay_publish_time);
        mMetaLayout = itemLayout.findViewById(R.id.layout_resource_meta);
        mLoadTime = itemLayout.findViewById(R.id.tv_load_time);
        mLoadTimeTit = itemLayout.findViewById(R.id.tv_load_time_title);
        mMoney = itemLayout.findViewById(R.id.tv_order_money);

        mLoadTimeTit.setTextColor(Color.GRAY);
        mLoadTime.setTextColor(Color.GRAY);

        itemLayout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MyOrderDetailInfoActivity.class);
                intent.putExtra("id", mOrderInfo.getId());
                mContext.startActivity(intent);
            }
        });
    }

    public void bindData(OrderStatusInfo info){
        if(info != null){
            mFromName.setText(mOrderInfo.getOrder_sg_from_city());
            mToName.setText(mOrderInfo.getOrder_sg_to_city());
            mSourceDistance.setText(mOrderInfo.getOrder_sg_distance() + "km");
            mPublishTime.setText(mOrderInfo.getOrder_createtime());

            mLoadTime.setText(mOrderInfo.getOrder_sg_loadtime());
            mPublishState.setText(mOrderInfo.getOrder_statusname());
            mMoney.setText(mOrderInfo.getTotal_final());

            mPublisher.setText(mOrderInfo.getOrder_sg_username() + mOrderInfo.getOrder_sg_phone());

            if(mOrderInfo.getOrder_sg_name()!= null && !mOrderInfo.getOrder_sg_name().equals("")){
                View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setText(mOrderInfo.getOrder_sg_name());
                mMetaLayout.addView(layout);
            }
            if(mOrderInfo.getOrder_sg_core_attr() != null && !mOrderInfo.getOrder_sg_core_attr().equals("")){
                View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setText(mOrderInfo.getOrder_sg_core_attr());
                mMetaLayout.addView(layout);
            }
            if(mOrderInfo.getOrder_sg_weight() != null && !mOrderInfo.getOrder_sg_weight().equals("")){
                View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setText(mOrderInfo.getOrder_sg_weight() + "吨");
                mMetaLayout.addView(layout);
            }
            if(mOrderInfo.getOrder_sg_truck_pre() != null){
                for(int index = 0; index < mOrderInfo.getOrder_sg_truck_pre().size(); index++){
                    View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                    TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                    textView.setText(mOrderInfo.getOrder_sg_truck_pre().get(index));
                    mMetaLayout.addView(layout);
                }
            }
        }
    }
}
